package org.example.springmvcdemo.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@RequestMapping("res")
//@RestController
//复合注解
@Controller
public class ResponseController {
    @RequestMapping("/index") //这里的 / 可以省略 但一般不省略
    public String index(){
        return "/html/index.html";//相对路径  /不能省略
    }
    @ResponseBody//返回数据  若注释掉 会报错 因为没有测试报错这么一个页面
    @RequestMapping("/indexData")
    public String indexData(){
        return "测试数据";
    }
    @ResponseBody
    @RequestMapping("/indexData2")
    public String indexData2(){
        return "<h1>我是火星人</h1>";
    }
    //返回josn
    @ResponseBody
    @RequestMapping("/getMap")
    public HashMap<String,String> getMap(){
        HashMap<String,String> map = new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v2");
        map.put("k3","v3");
        map.put("k4","v4");
        return map;
    }
    //设置状态码
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(418);
        return "设置状态码";
    }

    @ResponseBody
    @RequestMapping(value="/setContentType",produces = "applacation/josn")
    public String setContentType(){
        return "{\"OK\":1}";
    }


}
